package com.yj.jason.jsondemo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class TranslutionBehavior extends FloatingActionButton.Behavior{

    public TranslutionBehavior(Context context, AttributeSet set){
        super(context,set);

    }

    //关注垂直滚动
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        return axes== ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    private boolean isOut=false;

    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
        //向上隐藏，向下出来
        if(dyConsumed>0){//往上滑
            if(!isOut) {
                int translationY = ((CoordinatorLayout.LayoutParams) child.getLayoutParams()).bottomMargin + child.getMeasuredHeight();
                child.animate().translationY(translationY).setDuration(200).start();
                isOut=true;
            }
        }else if(dyConsumed<0){//往下滑
            if(isOut) {
                child.animate().translationY(0).setDuration(200).start();
                isOut=false;
            }
        }

    }


}
